package org.zsk.cs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zsk.cs.common.AppUtils;
import org.zsk.cs.dao.IdentityAuthDao;
import org.zsk.cs.dao.UserDao;
import org.zsk.cs.domain.po.IdentityAuth;
import org.zsk.cs.domain.po.RightObligation;
import org.zsk.cs.dao.RightObligationDao;
import org.zsk.cs.domain.po.User;
import org.zsk.cs.domain.vo.RightObligationVo;
import org.zsk.cs.service.RightObligationService;
import org.zsk.fastdev.assign.Assign;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 居民权利与义务 serviceImpl
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RightObligationServiceImpl extends ServiceImpl<RightObligationDao,RightObligation> implements RightObligationService {

    private final UserDao userDao;
    private final IdentityAuthDao identityAuthDao;
    private final RightObligationDao rightObligationDao;
    private final Assign assign;
    private final AppUtils appUtils;

    public RightObligationServiceImpl(UserDao userDao, IdentityAuthDao identityAuthDao, RightObligationDao rightObligationDao, Assign assign, AppUtils appUtils) {
        this.userDao = userDao;
        this.identityAuthDao = identityAuthDao;
        this.rightObligationDao = rightObligationDao;
        this.assign = assign;
        this.appUtils = appUtils;
    }

    @Override
    public Page<RightObligationVo> getWithUserNameByPage(RightObligationVo rightObligationVo) throws Exception {
        Page<RightObligation> page=new Page<>();
        assign.assign(rightObligationVo,page,"$current,$size");
        Page<RightObligation> rightObligationPage=page(page);
        return getRightObligationVoPage(rightObligationPage);
    }

    @Override
    public RightObligation districtHeadGetRightObligation() {
        return rightObligationDao.selectOne(new LambdaQueryWrapper<RightObligation>()
                .eq(RightObligation::getDistrictHeadId,appUtils.getDistrictHead().getId()));
    }

    @Override
    public RightObligation residentGetRightObligation() {
        //获取当前用户所在社区的社区长身份
        IdentityAuth districtHead=identityAuthDao.selectById(
                identityAuthDao.selectDistrictHeadIdByResidentId(
                        appUtils.getCurResident().getId()));
        return rightObligationDao.selectOne(new LambdaQueryWrapper<RightObligation>()
                .eq(RightObligation::getDistrictHeadId,districtHead.getId()));
    }

    @Override
    public boolean districtHeadSave(RightObligationVo rightObligationVo) throws Exception {
        //获取当前用户的社区长身份
        IdentityAuth districtHead=appUtils.getDistrictHead();
        RightObligation rightObligation=new RightObligation();
        assign.assign(rightObligationVo,rightObligation,"$content");
        rightObligation.setDistrictHeadId(districtHead.getId());
        return save(rightObligation);
    }

    private Page<RightObligationVo> getRightObligationVoPage(Page<RightObligation> rightObligationPage) throws Exception {
        Page<RightObligationVo> rightObligationVoPage = new Page<>();
        //合并搜索结果
        List<RightObligationVo> rightObligationVoList = rightObligationPage.getRecords()
                .stream().collect(LinkedList::new,(rightObligationVos, rightObligation) -> {
                    IdentityAuth districtHead = identityAuthDao.selectById(rightObligation.getDistrictHeadId());
                    User user = userDao.selectById(districtHead.getUid());
                    RightObligationVo curRightObligationVo = new RightObligationVo();
                    try {
                        assign.assign(rightObligation, curRightObligationVo);
                        curRightObligationVo.setUsername(user.getUsername());
                        rightObligationVos.add(curRightObligationVo);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
        },LinkedList::addAll);
        assign.assign(rightObligationPage, rightObligationVoPage);
        rightObligationVoPage.setRecords(rightObligationVoList);
        return rightObligationVoPage;
    }
}
